from matplotlib import pyplot as plt
from scipy.io import wavfile
from scipy.signal import spectrogram
import numpy as np

# 读取音频文件
sample_rate, data = wavfile.read(r'E:\实训\你好.wav')  # 替换为你的音频文件路径

# 计算频谱图
frequencies, times, spectrogram_data = spectrogram(data, fs=sample_rate)

# 绘制频率图
plt.figure(figsize=(10, 4))
plt.pcolormesh(times, frequencies, 10 * np.log10(spectrogram_data), shading='gouraud')
plt.colorbar(label='Intensity [dB]')
plt.title('Frequency Spectrum')
plt.ylabel('Frequency [Hz]')
plt.xlabel('Time [sec]')
plt.show()